package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.entity.Rooms;
import com.neuedu.yyzx.framework.Res;
import com.neuedu.yyzx.service.IRoomsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 房间信息 前端控制器
 * </p>
 *
 * @author Kobin
 * @since 2024-04-24
 */
@RestController
@RequestMapping("/rooms")
public class RoomsController {
    @Autowired
    IRoomsService roomsService;
    /**
     * http://localhost:8080/rooms/list
     * @param rooms
     * @return
     */
    @RequestMapping("/list")
    public Res<List<Rooms>> list(Rooms rooms) {

        QueryWrapper<Rooms> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0); // 添加条件：deleted 字段为 0
        return Res.ok(roomsService.list(queryWrapper));
    }
    //分页查询
    @RequestMapping("/page")
    public Res<Page> page(Page<Rooms> page, Rooms rooms) {
        QueryWrapper<Rooms> queryWrapper = new QueryWrapper<>();
//        if(rooms.getName()!= null && !"".equals(rooms.getName().trim())){
//            queryWrapper.like("name" ,rooms.getName() );
//        }
//        if(rooms.getRoomNumber()!= null && !"".equals(rooms.getRoomNumber().trim())){
//            queryWrapper.like("room_number" ,rooms.getRoomNumber() );
//        }

        LambdaQueryWrapper<Rooms> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Rooms::getDeleted, 0);
        if(rooms.getName()!= null && !"".equals(rooms.getName().trim())) {
            lambdaQueryWrapper.like(Rooms::getName, rooms.getName());
        }
        if(rooms.getRoomNumber()!= null && !"".equals(rooms.getRoomNumber().trim())) {
            lambdaQueryWrapper.like(Rooms::getRoomNumber, rooms.getRoomNumber());
        }

        return Res.ok(roomsService.page(page,lambdaQueryWrapper));
    }

    @RequestMapping("/saveOrUpdate")
    public Res  saveOrUpdate( @RequestBody Rooms rooms){
        return  Res.ok(roomsService.saveOrUpdate(rooms));
    }


    @RequestMapping("/removeById/{id}")
    public Res  removeById(@PathVariable int id){
        Rooms rooms = roomsService.getById(id);
        if (rooms != null){
            rooms.setDeleted(1);
        }else {
            return Res.failed("删除失败，找不到对应的记录");
        }
        boolean success = roomsService.saveOrUpdate(rooms);
        return  Res.ok(success);
    }



    @RequestMapping("/getById/{id}")
    public Res<Rooms> getById(@PathVariable  int id){
        return  Res.ok(roomsService.getById(id));
    }

}
